package com.example.qfsx.bean;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.qfsx.R;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ItemAdapter extends BaseAdapter {
    private Context context;
    private List<CartItem2> itemList;

    public ItemAdapter(Context context, List<CartItem2> itemList) {
        this.context = context;
        this.itemList = itemList;
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.cart_item, parent, false);
            holder = new ViewHolder();
            holder.imageView = convertView.findViewById(R.id.goods_img);
            holder.textView = convertView.findViewById(R.id.goods_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        CartItem2 item = itemList.get(position);
        holder.textView.setText(item.getGoods_name());
        // 使用Picasso加载网络图片
        Picasso.get().load(item.getGoods_img2()).into(holder.imageView);

        return convertView;
    }

    private static class ViewHolder {
        ImageView imageView;
        TextView textView;
    }
}